package com.web.activiti.controller;


import com.web.activiti.pojo.ActiveUser;
import com.web.activiti.pojo.Employee;
import com.web.activiti.pojo.TreeMenu;
import com.web.activiti.service.EmployeeService;
import com.web.activiti.service.SysService;
import com.web.activiti.utils.Contants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private SysService sysService;

    //用户验证,不是真正的登录逻辑，只是失败后接收返回的错误消息
    /**
     * 通过shiro认证之后如果出错会在此处产生错误信息
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request, Model model){
        String exceptionName = (String) request.getAttribute("shiroLoginFailure");
        if (exceptionName != null) {
            if (UnknownAccountException.class.getName().equals(exceptionName)) {
                model.addAttribute("errorMsg", "用户账号不存在");
            } else if (IncorrectCredentialsException.class.getName().equals(exceptionName)) {
                model.addAttribute("errorMsg", "密码不正确");
            } else if("randomcodeError".equals(exceptionName)) {
                model.addAttribute("errorMsg", "验证码不正确");
            }
            else {
                System.out.println(exceptionName);
                model.addAttribute("errorMsg", "未知错误");
            }
        }
//        System.out.println("认证失败");
        return "login";
    }

    /**
     * 清空缓存达到退出的目的
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }


    /**
    登录成功跳转到主界面
     */
    @RequestMapping("/main")
    public String main(ModelMap model,HttpServletRequest request){
        System.out.println("登录");
        ActiveUser activeUser= (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        HttpSession session = request.getSession();
        session.setAttribute(Contants.GLOBAL_SESSION_ID, activeUser);
        model.addAttribute("activeUser",activeUser);
        return "index";
    }


    public void  fun(){
        String salt="eteokues";
        Md5Hash md4=new Md5Hash("123", salt);
        System.out.println(md4.toString());//fd4613f37bb1805db10acf1768fb8d76
    }
}
